﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PMSSys
{
    public partial class TreatmentView : Form
    {
        public TreatmentView(string tplan_id, treatmentController tmcontroller, int p_id)
        {
            InitializeComponent();
            tmObject = new TM();
            tcontrol = tmcontroller;
            this.p_id = p_id;
            if (String.IsNullOrEmpty(tplan_id))
            {
                addMode = true;
                this.Text = "Add Treatment";
            }
            else
            {
                this.Text = "View Treatment";
                tmObject.TmID = int.Parse(tplan_id);
                tmID = tmObject.TmID;         
                tcontrol.objectFiller(tmObject);
                dpTreatment.Value = tmObject.TMDate;
                cbType.Text = tmObject.TestType;
                tbNotes.Text = tmObject.Comments;
                isModified = false;
            }

        }
        private bool isModified = false;
        private TM tmObject;
        private treatmentController tcontrol;
        private bool addMode = false;
        private int tmID, p_id;
        private void closeLogic(object sender, EventArgs e)
        {
            this.Close();
        }

        private void modifiedChecker(object sender, EventArgs e)
        {
            isModified = true;
        }

        private void TreatmentView_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isModified)
            {
                DialogResult res = uiUtility.confirmSaveDialog();
                if (res != DialogResult.Cancel)
                {
                    if (res == DialogResult.Yes)
                    {
                        if (guiChecker())
                        {
                            if (addMode)
                            {
                                tcontrol.add(addAttributes());
                            }
                            else
                            {
                                tcontrol.update(addAttributes());
                            }
                        }
                        else
                        { 
                            e.Cancel = true;
                        }
                    }
                }
                else
                    e.Cancel = true;
            }

        }

        private void TreatmentView_Load(object sender, EventArgs e)
        {
            ToolTip t1 = uiUtility.tooltips();
            t1.SetToolTip(this.button6, "Back");
        }
        public TM addAttributes()
        {
            tmObject.PatientID = this.p_id;
            tmObject.TmID = tmID;
            tmObject.TestType = cbType.Text;
            tmObject.TMDate = dpTreatment.Value;
            tmObject.Comments = tbNotes.Text;
            return tmObject;                           
        }
        private bool guiChecker()
        {
            DateTime dateNow = DateTime.Now;
            int cmpDate = DateTime.Compare(dpTreatment.Value, dateNow);
            if (cbType.Text == String.Empty)
            {
                MessageBox.Show("Add treatment type", "Treatment Type", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            if (cmpDate > 0)
            {
                DialogResult res = uiUtility.validDate("Payment Date");
                return false;
            }
            return true;
        }
    }
}
